var util = require("../util"), constants = require("./constants"), TIMING_KEYS = constants.TIMING_KEYS;

module.exports = function() {
    var t = util.win || {}, n = t.performance;
    if (!n || "object" != typeof n) return null;
    var e = {}, i = n.timing || {}, a = Date.now(), r = 1;
    if ("function" == typeof t.PerformanceNavigationTiming) {
        var o = n.getEntriesByType("navigation")[0];
        o && (i = o, r = 2);
    }
    util.each({
        dns: [ 3, 2 ],
        tcp: [ 5, 4 ],
        ssl: [ 5, 17 ],
        ttfb: [ 7, 6 ],
        trans: [ 8, 7 ],
        dom: [ 10, 8 ],
        res: [ 14, 12 ],
        firstbyte: [ 7, 2 ],
        fpt: [ 8, 1 ],
        tti: [ 10, 1 ],
        ready: [ 12, 1 ],
        load: [ 14, 1 ]
    }, function(t, n) {
        var a = i[TIMING_KEYS[t[1]]], o = i[TIMING_KEYS[t[0]]];
        if (2 === r || a > 0 && o > 0) {
            var I = Math.round(o - a);
            I >= 0 && I < 6e5 && (e[n] = I);
        }
    });
    var I = t.navigator.connection, l = n.navigation || {};
    e.ct = I ? I.effectiveType || I.type : "";
    var f = I ? I.downlink || I.downlinkMax || I.bandwidth || null : null;
    if ((f = f > 999 ? 999 : f) && (e.bandwidth = f), e.navtype = 1 === l.type ? "Reload" : "Other", 
    1 === r && i[TIMING_KEYS[16]] > 0 && i[TIMING_KEYS[1]] > 0) {
        var u = i[TIMING_KEYS[16]] - i[TIMING_KEYS[1]];
        u >= 0 && u < 36e5 && (e.fpt = u);
    }
    return 1 === r && i[TIMING_KEYS[1]] > 0 ? e.begin = i[TIMING_KEYS[1]] : 2 === r && e.load > 0 ? e.begin = a - e.load : e.begin = a, 
    e;
};